<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.net.URLEncoder"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <%
  	if("post".equals(request.getMethod().toLowerCase())){     
  	request.setCharacterEncoding("UTF-8");        //request代表客户端请求
  	response.setCharacterEncoding("UTF-8");      //response代表响应
  	//英文默认用AsCall编码，中文默认Uncoide编码，所以传中文时用UTF-8编码，接受时utf-8解码
  	Cookie userCookie=new Cookie("user",URLEncoder.encode(request.getParameter("username"),"UTF-8")); //获取提交的参数值
  	Cookie visitCookie=new Cookie("visit","0"); //初始的登录次数为0
     //userCookie.setSecure(true);
  	//visitCookie.setSecure(true);
  	visitCookie.setPath("/");
  //	visitCookie.setSecure(true);
  	userCookie.setPath("/");
  	userCookie.setSecure(true);
  	session.setAttribute("time",new Date());      //request为服务器服务
  	response.addCookie(userCookie);               //cookie是保存在客户端的，所以往客户端发送cookie
  	response.addCookie(visitCookie);              //响应 ，给客户端发送cookie    当前web应用 所有的页面都能获取这些cookie
  	response.sendRedirect(basePath+"cookie.jsp");
  	}
   %>
  <body>
  <form action="${pageContext.request.requestURL }" method="post">      <!-- 提交到当前路径 -->
  <input type="text" name="username" >
  <input type="submit"  value="提交" />
  </form>
  <%=basePath
 
   %>               <!-- http://localhost:8090/WEB01/ -->
   <%= path %>       <!-- /Web01 -->
  </body>
</html>
